package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkScoreGraphController.RespModel;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkScoreGraphController.ScoreSheet;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseScoreGraphService;

@Service
public class HomeworkInfoAnalyseScoreGraphServiceImpl implements HomeworkInfoAnalyseScoreGraphService {

    @Override
    public RespModel convertRespModel(List<HomeworkTaskReport> homeworkTaskReports) {
        // 参数验证
        if (homeworkTaskReports == null || homeworkTaskReports.size() == 0) { return null; }
        // return
        RespModel respModel = new RespModel();
        List<ScoreSheet> scoreSheets = new ArrayList<ScoreSheet>();
        // ##
        // 成绩分布分布图
        List<Integer> scoreTypes = new ArrayList<>();
        scoreTypes.add(1); // 100%
        scoreTypes.add(2); // 90% ~ 99%
        scoreTypes.add(3); // 80% ~ 89%
        scoreTypes.add(4); // 60% ~ 79%
        scoreTypes.add(5); // < 60%
        scoreTypes.add(0); // 未交
        // ##
        scoreTypes.forEach(type -> {
            // ##
            ScoreSheet scoreSheet = new ScoreSheet();
            scoreSheet.scorePurTitle = this.getScoreType(type);
            scoreSheet.scorePeopleTotal = this.getScorePeople(type, homeworkTaskReports);
            scoreSheets.add(scoreSheet);
        });
        // 班级知识点掌握程度报表
        respModel.scoreSheets = scoreSheets;
        // 总人数
        respModel.total = homeworkTaskReports.size();
        // 班级作业综合评分
        respModel.synthesizeScore = this.synthesizeScore(homeworkTaskReports);
        return respModel;
    }

    // 成绩分布名称
    public String getScoreType(int type) {
        if (type == 1) {
            return "100%";
        } else if (type == 2) {
            return "90%~99%";
        } else if (type == 3) {
            return "80%~89%";
        } else if (type == 4) {
            return "60%~79%";
        } else if (type == 5) {
            return "<60%";
        } else {
            return "未交";
        }
    }

    // 人数
    public int getScorePeople(int type, List<HomeworkTaskReport> homeworkTaskReports) {
        if (type == 1) {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                /*if (homeworkTaskReport.getTaskType().equals("1")) {
                    continue;
                }*/
                if (homeworkTaskReport.getScoreRate() >= 100) {
                    n++;
                }
            }
            return n;
        } else if (type == 2) {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                /*if (homeworkTaskReport.getTaskType().equals("1")) {
                    continue;
                }*/
                if (homeworkTaskReport.getScoreRate() >= 90 && homeworkTaskReport.getScoreRate() < 100
                        && (homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)) {
                    n++;
                }
            }
            return n;
        } else if (type == 3) {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                /*if (homeworkTaskReport.getTaskType().equals("1")) {
                    continue;
                }*/
                if (homeworkTaskReport.getScoreRate() >= 80 && homeworkTaskReport.getScoreRate() < 90
                        && (homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)) {
                    n++;
                }
            }
            return n;
        } else if (type == 4) {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                /*if (homeworkTaskReport.getTaskType().equals("1")) {
                    continue;
                }*/
                if (homeworkTaskReport.getScoreRate() >= 60 && homeworkTaskReport.getScoreRate() < 80
                        && (homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)) {
                    n++;
                }
            }
            return n;
        } else if (type == 5) {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                /*if (homeworkTaskReport.getTaskType().equals("1")) {
                    continue;
                }*/
                if (homeworkTaskReport.getScoreRate() < 60 && (homeworkTaskReport.getStatusOfFinished() == 1
                        || homeworkTaskReport.getStatusOfFinished() == 2)) {
                    n++;
                }
            }
            return n;
        } else {
            int n = 0;
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                // -1-逾期未提交,0-未做,1-完成 ,2-已批阅,3-答题中
                if (homeworkTaskReport.getStatusOfFinished() == -1 || homeworkTaskReport.getStatusOfFinished() == 0
                        || homeworkTaskReport.getStatusOfFinished() == 3) {
                    n++;
                }
            }
            return n;
        }
    }

    // 班级作业综合评分
    public double synthesizeScore(List<HomeworkTaskReport> homeworkTaskReports) {
        // ##
        double classAverageScore = 0f;
        double averageScore = 0f;
        double n = 0;
        for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
            averageScore = averageScore + homeworkTaskReport.getScoreRate();
            n++;
        }
        classAverageScore = averageScore / n;
        if (classAverageScore > 90) {
            return 5d;
        } else if (classAverageScore > 85) {
            return 4.5d;
        } else if (classAverageScore > 80) {
            return 4d;
        } else if (classAverageScore > 75) {
            return 3.5d;
        } else if (classAverageScore > 70) {
            return 3d;
        } else if (classAverageScore > 65) {
            return 2.5d;
        } else if (classAverageScore > 60) {
            return 2d;
        } else if (classAverageScore > 55) {
            return 1.5d;
        } else if (classAverageScore > 50) {
            return 1d;
        } else {
            return 0d;
        }
    }
}
